<!--点名记录-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="上课日期" prop="classDate">
        <el-date-picker
          v-model="classDate"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="classDateRangeChange"
          value-format="YYYY-MM-DD"
          style="width: 240px"
        />
      </el-form-item>

      <el-form-item label="所在班级" prop="courseId">
        <el-select
          v-model="queryParams.classId"
          placeholder="请选择所在班级"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="主班老师" prop="teacher">
        <el-select
          v-model="queryParams.teacher"
          placeholder="请选择主班老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="助教老师" prop="assistant">
        <el-select
          v-model="queryParams.assistant"
          placeholder="请选择助教老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="点名日期" prop="callDate">
        <el-date-picker
          v-model="callDate"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="callDateRangeChange"
          value-format="YYYY-MM-DD"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="授课课程" prop="courseId">
        <el-select
          v-model="queryParams.courseId"
          placeholder="请选择授课课程"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="授课方式" prop="lessonType">
        <el-select
          v-model="queryParams.lessonType"
          placeholder="请选择授课方式"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in edu_teaching_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getList"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button plain icon="Download" @click="handleExport('2')">
          导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip class="box-item" placement="right" effect="light">
          <template #content>
            按学员导出：以学员为单位导出上课记录，可用于统计
            学员课时/课消，学员到课情况等
          </template>
          <div class="info">
            <el-icon><InfoFilled /></el-icon>
          </div>
        </el-tooltip>
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" border :max-height="tableHeight">
      <el-table-column
        label="点名时间"
        align="center"
        prop="callTime"
        width="160"
      >
        <template #default="scope">
          {{
            scope.row.callTime &&
            scope.row.callTime.slice(0, 10) +
              "    " +
              scope.row.callTime.slice(11, 19)
          }}
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="授课课程" align="center" prop="courseNameC" />
      <el-table-column
        label="上课时间"
        align="center"
        prop="classDate"
        width="240"
      >
        <template #default="scope">
          {{
            scope.row.weekNum
              ? scope.row.classDate.slice(0, 10) +
                "(周" +
                {
                  1: "一",
                  2: "二",
                  3: "三",
                  4: "四",
                  5: "五",
                  6: "六",
                  7: "日",
                }[scope.row.weekNum] +
                ")" +
                "  " +
                scope.row.classStartTime +
                "~" +
                scope.row.classEndTime
              : scope.row.classDate.slice(0, 10) +
                "  " +
                scope.row.classStartTime +
                "~" +
                scope.row.classEndTime
          }}
        </template>
      </el-table-column>
      <el-table-column label="主班老师" align="center" prop="teacherName" />
      <el-table-column
        label="助教老师"
        align="center"
        prop="assistantName"
        width="160"
      />
      <el-table-column label="授课课时" align="center" prop="classHourPay" />
      <el-table-column label="课消金额" align="center">
        <template #default="scope"> ¥ {{ scope.row.amount }} </template>
      </el-table-column>
      <el-table-column label="上课内容" align="center" prop="description" />
      <el-table-column label="到课状态" align="center" prop="state" />
      <el-table-column
        label="操作"
        align="center"
        width="140"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">
            查看点名详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
      size="small"
    />
  </div>
  <RecordDetail
    ref="recordRef"
    :form="form"
    :classTitle="selectedItem.className"
    :orderNo="selectedItem.orderNo"
    v-bind="$props"
    @refreshPage="getList"
    :isDetail="false"
  />
</template>

<script setup>
import {
  getRollCallListAPI,
  exportOrderListSecondAPI,
  getClassListAPI,
} from "@/api/registrar/class";
import { getCourseListAPI } from "@/api/registrar/course";
import { getPeopleList } from "@/api/studentManagement/index";

import RecordDetail from "@/views/registrar/class/detail/components/recordDetail.vue";
//接收学员ID
const route = useRoute();
const router = useRouter();
const studentsId = route.params.studentsId;
const { proxy } = getCurrentInstance();
const { edu_teaching_mode } = proxy.useDict("edu_teaching_mode");

const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    classStartTime: null,
    classEndTime: null,
    callStartTime: null,
    callEndTime: null,
    classId: null,
    teacher: null,
    assistant: null,
    courseId: null,
    lessonType: null,
  },
});

const { queryParams } = toRefs(data);
const tHeight = window.innerHeight - 410;
const tableHeight = ref(tHeight > 0 ? tHeight : 50);

const list = ref([]);
const classList = ref([]);
const teacherList = ref([]);
const courseList = ref([]);
const callDate = ref(null);
const classDate = ref(null);

// 详情
const recordRef = ref(null);
const selectedItem = ref({});

// 事件------------------------------------------------------------------------------------------------------------

function classDateRangeChange(event) {
  classDate.value = event;
  queryParams.value.classStartTime = event ? event[0] : null;
  queryParams.value.classEndTime = event ? event[1] : null;
}

function callDateRangeChange(event) {
  callDate.value = event;
  queryParams.value.callStartTime = event ? event[0] : null;
  queryParams.value.callEndTime = event ? event[1] : null;
}
// 导出
function handleExport(exportType) {
  exportOrderListSecondAPI({ exportType }).then((res) => {
    let blob = new Blob([res], {
      type: "application/vnd.ms-excel;charset=utf-8",
    });
    const url = window.URL.createObjectURL(blob),
      link = document.createElement("a");
    link.href = url;
    if (exportType === "1") link.download = "点名记录表（按班级）";
    else link.download = "点名记录表（按人名）";
    link.click();
    URL.revokeObjectURL(url); // 释放内存
    proxy.$modal.msgSuccess("导出成功");
  });
}

// 详情
function handleDetail(row) {
  //proxy.$modal.msgSuccess("暂未开发");
  selectedItem.value = row;
  setTimeout(() => {
    recordRef.value.getRollCallDetail(row);
  });
}

// 课后点评
function handleComment() {
  proxy.$modal.msgSuccess("暂未开发");
}

function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    classStartTime: null,
    classEndTime: null,
    callStartTime: null,
    callEndTime: null,
    classId: null,
    teacher: null,
    assistant: null,
    courseId: null,
    lessonType: null,
  };
  classDate.value = null;
  callDate.value = null;

  getList();
}

// 方法------------------------------------------------------------------------------------------

// 课程
function getCourseList() {
  getCourseListAPI({
    pageSize: 9999,
    pageNum: 1,
    open: 1,
  }).then((response) => {
    courseList.value = response.rows;
    loading.value = false;
  });
}

// 班级
function getClassList() {
  loading.value = true;
  getClassListAPI({ pageSize: 9999, pageNum: 1 }).then((response) => {
    classList.value = response.rows;
    loading.value = false;
  });
}

// 获取教师列表
function getTeacherList() {
  loading.value = true;
  getPeopleList({ isTeach: 1 }).then((response) => {
    teacherList.value = response.data;
    loading.value = false;
  });
}

function initPicker() {
  getCourseList();
  getClassList();
  getTeacherList();
}

function getList() {
  loading.value = true;
  getRollCallListAPI({
    ...queryParams.value,
    listType: "1",
    studentId: studentsId,
    org: 1,
  }).then((response) => {
    list.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 调用-------------------------------------------------------------------------------------------------------------------
initPicker();
getList();
</script>

<style lang="scss" scoped>
.info {
  display: inline-flex;
  align-items: center;
  height: 32px;
}
</style>
